﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FM.Storage.Abstract;
using FM.Storage.Models;
using Microsoft.AspNetCore.Mvc;

namespace Demo.Controllers
{
    public class FileController : Controller
    {
        private readonly ILocalFileManager _localFileManager;

        public FileController(ILocalFileManager localFileManager)
        {
            _localFileManager = localFileManager;
        }

      
        [HttpPost]
        public async Task<ActionResult<UploadResult>> UploadFile()
        {
            var request = Request;
            var data = Request.Form.Files["data"];

            var data2 = Request.Form.Files;
          
            int.TryParse(Request.Form["chunkNumber"], out int chunkNumber);

            int.TryParse(Request.Form["totalChunks"], out int totalChunks);
            //httpContext.Request.Query.TryGetValue("identifier", out var identifiers);
            //if (chunkNumber == 0 || string.IsNullOrEmpty(identifiers))
            //{
            //    return new UploadResult()
            //    {
            //        msg = "上传为文件为空"
            //    };
            //}
            //else
            //{
            //    var chunkFilename = _localFileManager.GetChunkFilename(chunkNumber, identifiers);
            //    if (!System.IO.File.Exists(chunkFilename))
            //    {
            //        return new UploadResult()
            //        {
            //            msg = "文件名不存在"
            //        };
            //    }
            //}
            if (totalChunks > 1)//chunkUpload
            {
                var result = await _localFileManager.ChunkUploadAsync(request);
                return result;
            }
            else
            {
                var result = await _localFileManager.UploadAsyncAsync(request);
                return result;
            }

        }
    }
}